package com.zmq.filmsystem.controller;

import com.zmq.filmsystem.entity.Canema;
import com.zmq.filmsystem.entity.User;
import com.zmq.filmsystem.service.impl.QiniuService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Controller
@RequestMapping("/test")
public class TestController {
    @Autowired
    QiniuService qiniuService;

    private Logger logger = LoggerFactory.getLogger(TestController.class);

    @GetMapping("/showFile")
    public String testShow(){
        return "test/testFile";
    }

    @PostMapping("/addFile")
    @Transactional
    public String testAddFile(@RequestParam("file") MultipartFile file,
                            Model model)  {

        if(file.isEmpty()) {
            model.addAttribute("msg", "文件为空");
        }

        try {
            String fileUrl = qiniuService.saveImage(file);
            model.addAttribute("fileUrl", fileUrl);
            logger.info("图片url = "+fileUrl);
        } catch (IOException e) {
            e.printStackTrace();
            model.addAttribute("msg", "添加失败!");
            return "test/testList";
        }

        return "test/testList";

    }


}
